import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_oldman_health/api/auth_service.dart';
import 'package:flutter_oldman_health/api/constant.dart';
import 'package:flutter_oldman_health/models/response_data_model.dart';
import 'dart:convert';
import 'package:flutter_oldman_health/models/user_model.dart';
import 'package:flutter_oldman_health/pages/login_page.dart';
import 'package:flutter_oldman_health/utils/utils.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AccountPage extends StatelessWidget {
  UserInfo userInfo;
  Uint8List bytes;

  AccountPage({Key k, UserInfo userInfo}) : super(key: k) {
    this.userInfo = userInfo;
    if (null != userInfo) {
      bytes = Base64Decoder().convert(userInfo.qrCodeImage);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      //padding: EdgeInsets.only(top: 12),
      color: Constant.backgroundColor,
      child: Column(
        children: <Widget>[
          _getTitle(context),
          Container(
              margin: EdgeInsets.only(top: 20),
              child: Text(
                "姓名: ${userInfo?.name}",
                textAlign: TextAlign.center,
                style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 36,
                    color: Colors.black),
              )),
          Container(
              margin: EdgeInsets.fromLTRB(0, 20, 0, 40),
              child: Text(
                "电话: ${userInfo?.phone}",
                textAlign: TextAlign.center,
                style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 22,
                    color: Colors.grey),
              )),
          (null == bytes)
              ? (Image.asset("images/ic_family_member_head.png",
                  fit: BoxFit.fill, height: 280, width: 280))
              : (Image.memory(bytes,
                  fit: BoxFit.fill, height: 280, width: 280)),
          // Image.network(
          //   imageUri,
          //   fit: BoxFit.fill,
          //   height: 280,
          //   width: 280,
          // ),
          Container(
              margin: EdgeInsets.only(top: 20),
              child: Text(
                "我的二维码",
                textAlign: TextAlign.center,
                style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 22,
                    color: Colors.black),
              )),
          //http://www.cppcns.com/ruanjian/android/361621.html 三方库英文转汉文
          Container(
              margin: EdgeInsets.fromLTRB(0, 20, 0, 10),
              child: Text(
                "扫描上方二维码添加好友",
                textAlign: TextAlign.center,
                style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 17,
                    color: Colors.grey),
              )),
        ],
      ),
    );
  }

  Widget _getTitle(BuildContext context, [String title = "账号"]) {
    return Container(
        color: Constant.appbarColor,
        padding: EdgeInsets.fromLTRB(16, 46, 16, 8),
        child: FractionallySizedBox(
          widthFactor: 1,
          child: Stack(
            alignment: Alignment.center,
            children: [
              Positioned(
                left: 2,
                child: GestureDetector(
                      behavior: HitTestBehavior.translucent,
                  onTap: () {
                    Navigator.pop(context);
                  },
                  child: Container(
                          child: Icon(Icons.arrow_back_ios),
                          padding: EdgeInsets.fromLTRB(15, 10, 20, 10)),
                ),
              ),
              Text(title,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                      fontSize: 22,
                      color: Colors.black87,
                      fontWeight: FontWeight.normal)),
              Positioned(
                  right: 2,
                  child: GestureDetector(
                      behavior: HitTestBehavior.translucent,
                    onTap: () {
                      showRemindDialog(context).then((value) {
                        print(value);
                      });
                    },
                    child: Container(
                      padding: EdgeInsets.fromLTRB(8, 2, 8, 2),
                      child: Text(
                        "注销",
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 18,
                            fontWeight: FontWeight.normal),
                      ),
                      decoration: BoxDecoration(
                          color: Color(0xff41BAB0),
                          borderRadius: BorderRadius.all(Radius.circular(8))),
                    ),
                  ))
            ],
          ),
        ));
  }

  Future showRemindDialog(BuildContext context) async {
    var result = await showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
              title: Text("账号注销"),
              content: Text("确定注销当前账号（相关数据将被清除）？"),
              actions: <Widget>[
                ElevatedButton(
                    onPressed: () {
                      Navigator.pop(context);
                    },
                    child:
                        Padding(padding: EdgeInsets.all(8), child: Text("取消")),
                    style: ButtonStyle(
                        textStyle:
                            MaterialStateProperty.all(TextStyle(fontSize: 17)),
                        shape: MaterialStateProperty.all(StadiumBorder(
                            side: BorderSide(
                          //设置 界面效果 圆角弧度
                          style: BorderStyle.solid, color: Colors.grey[400]
                        ))))),
                ElevatedButton(
                    onPressed: () async {
                      var response = await AuthService.cancellation();
                      if (null != response && response.statusCode == 200) {
                        ResponseDataModel<int> value =
                            ResponseDataModel<int>.fromJson(response.data);
                        if (value.success()) {
                          final prefs = await SharedPreferences.getInstance();
                          prefs.remove("phone");
                          prefs.remove("password");

                          prefs.remove("token");
                          prefs.remove("userId");
                          prefs.remove("user_nick");
                          prefs.remove("user_mobile");

                          Navigator.of(context).pushAndRemoveUntil(
                              MaterialPageRoute(
                                  builder: (context) => LoginPage()),
                              (route) => route == null);
                          // Navigator.push(
                          //     context,
                          //     MaterialPageRoute(
                          //         builder: (context) => LoginPage()));
                        } else {
                          EasyLoading.showInfo(value.msg);
                        }
                      } else {
                        EasyLoading.showInfo("注销失败");
                      }
                    },
                    child:
                        Padding(padding: EdgeInsets.all(8), child: Text("确定")),
                    style: ButtonStyle(
                        textStyle:
                            MaterialStateProperty.all(TextStyle(fontSize: 17)),
                        shape: MaterialStateProperty.all(StadiumBorder(
                            side: BorderSide(
                          //设置 界面效果 圆角弧度
                          style: BorderStyle.solid,
                                color: Colors.grey[400]
                        ))))),
              ]);
        });
    return result;
  }
}
